<?php
/**
 *   Joomdiet - administrator/components/models/editvariable.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class JoomdietModelEditVariable extends JModel
{
    var $_variable;
    
    function __construct()
    {
        parent::__construct();
        
        $array = JRequest::getVar('cid', 0, '', 'array');
        $this->setId((int)$array[0]);
    }
    
    function setId($id)
    {
        $this->_id = $id;
        $this->_variable = null;
    }
    
    function get($property, $default=null)
    {
        if ($this->_loadVariable()) {
            if (isset($this->_variable->$property)) {
                return $this->_variable->$property;
            }
        }
        
        return $default;
    }
    
    function &getInfant()
    {
        $query = 'SELECT * FROM #__joomdiet_Nutritional WHERE variable_id='.$this->_id.' AND population_group = 1';
        $this->_db->setQuery($query);
        $suggestions = $this->_db->loadObjectList();
        return $suggestions;
    }
    
    function &getChildren()
    {
        $query = 'SELECT * FROM #__joomdiet_Nutritional WHERE variable_id='.$this->_id.' AND population_group = 2';
        $this->_db->setQuery($query);
        $suggestions = $this->_db->loadObjectList();
        return $suggestions;
    }
    
    function &getMen()
    {
        $query = 'SELECT * FROM #__joomdiet_Nutritional WHERE variable_id='.$this->_id.' AND population_group = 3';
        $this->_db->setQuery($query);
        $suggestions = $this->_db->loadObjectList();
        return $suggestions;
    }
    
    function &getWomen()
    {
        $query = 'SELECT * FROM #__joomdiet_Nutritional WHERE variable_id='.$this->_id.' AND population_group = 4';
        $this->_db->setQuery($query);
        $suggestions = $this->_db->loadObjectList();
        return $suggestions;
    }
    
    function &getLactation()
    {
        $query = 'SELECT * FROM #__joomdiet_Nutritional WHERE variable_id='.$this->_id.' AND population_group = 5';
        $this->_db->setQuery($query);
        $suggestions = $this->_db->loadObjectList();
        return $suggestions;
    }
    
    function &getGestation()
    {
        $query = 'SELECT * FROM #__joomdiet_Nutritional WHERE variable_id='.$this->_id.' AND population_group = 6';
        $this->_db->setQuery($query);
        $suggestions = $this->_db->loadObjectList();
        return $suggestions;
    }
    
    function &getVariable()
    {
        if ($this->_loadVariable()) {
        } else $this->_initVariable();
        
        return $this->_variable;
    }
    
    function _loadVariable()
    {
        if (empty($this->_variable))
        {
            $query = 'SELECT * FROM #__joomdiet_Variable WHERE id='.$this->_id;
            $this->_db->setQuery($query);
            $this->_variable = $this->_db->loadObject();
            
            return (boolean) $this->_variable;
        }
    }
    
    function _initVariable()
    {
        if (empty($this->_variable)) {
            $variable = new stdClass();
            $variable->id = 0;
            $variable->name = null;
            $variable->unit = null;
            $variable->description = null;
            $variable->min_value = null;
            $variable->max_value = null;
            $variable->category = 0;
            
            $this->_variable = $variable;
            return (boolean) $this->_variable;
        }
        return true;
    }
    
    function store()
    {
        $row =& JTable::getInstance('variable', 'Table');

        $id = JRequest::getString('variable_id');
        $name = JRequest::getString('variable_name');
        $units = JRequest::getString('variable_units');
        $description = JRequest::getString('variable_description');
        $min_value = JRequest::getString('variable_min_value');
        $max_value = JRequest::getString('variable_max_value');
        $category = JRequest::getString('variable_category');

        if ($id) $row->set('id', $id);
        $row->set('name', $name);
        $row->set('unit', $units);
        $row->set('description', $description);
        $row->set('min_value', $min_value);
        $row->set('max_value', $max_value);
        $row->set('category', $category);
        
        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
        
        if (!$row->store()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
        
        // check new entry
        if (!isset($id)) $id = $row->get('id');
        $query = 'SELECT count(*) FROM #__joomdiet_Nutritional WHERE variable_id='.$id;
        $this->_db->setQuery($query);
        if ($this->_db->loadResult() == 0) {
        // creating rdi table
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 1, 0, 1, '.JRequest::getString('infant0_1').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 2, 1, 3, '.JRequest::getString('children1_3').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 2, 4, 8, '.JRequest::getString('children4_8').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 3, 9, 13, '.JRequest::getString('men9_13').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 3, 14, 18, '.JRequest::getString('men14_18').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 3, 19, 70, '.JRequest::getString('men19_70').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 3, 71, 300, '.JRequest::getString('men71_300').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 4, 9, 13, '.JRequest::getString('women9_13').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 4, 14, 18, '.JRequest::getString('women14_18').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 4, 19, 70, '.JRequest::getString('women19_70').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 4, 71, 300, '.JRequest::getString('women71_300').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 5, 0, 18, '.JRequest::getString('lactation0_18').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 5, 19, 50, '.JRequest::getString('lactation19_50').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 6, 0, 18, '.JRequest::getString('gestation0_18').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'INSERT INTO #__joomdiet_Nutritional (variable_id, population_group, age_min, age_max, suggested_value) ';
            $query .= 'VALUES ('.$id.', 6, 19, 50, '.JRequest::getString('gestation19_50').')';
            $this->_db->setQuery($query);
            $this->_db->Query();
        } else {
        // updating rdi table
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('infant0_1').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=1 AND age_min=0 AND age_max=1';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('children1_3').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=2 AND age_min=1 AND age_max=3';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('children4_8').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=2 AND age_min=4 AND age_max=8';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('men9_13').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=3 AND age_min=9 AND age_max=13';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('men14_18').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=3 AND age_min=14 AND age_max=18';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('men19_70').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=3 AND age_min=19 AND age_max=70';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('men71_300').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=3 AND age_min=71 AND age_max=300';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('women9_13').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=4 AND age_min=9 AND age_max=13';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('women14_18').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=4 AND age_min=14 AND age_max=18';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('women19_70').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=4 AND age_min=19 AND age_max=70';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('women71_300').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=4 AND age_min=71 AND age_max=300';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('lactation0_18').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=5 AND age_min=0 AND age_max=18';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('lactation19_50').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=5 AND age_min=19 AND age_max=50';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('gestation0_18').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=6 AND age_min=0 AND age_max=18';
            $this->_db->setQuery($query);
            $this->_db->Query();
            $query = 'UPDATE #__joomdiet_Nutritional SET suggested_value="'.JRequest::getString('gestation19_50').'" ';
            $query .= ' WHERE variable_id='.$id.' AND population_group=6 AND age_min=19 AND age_max=50';
            $this->_db->setQuery($query);
            $this->_db->Query();
        }
        return true;
    }
}
